<?php

$__FILEPATH__ = dirname(__FILE__)."/";
require_once($__FILEPATH__."func_common.php");
require_once($__FILEPATH__."class_def.php");
require_once($__FILEPATH__."conf/info.php");

header("Content-Type: text/html; charset=UTF-8");

session_start();

$mysqli = GetMysqliInstance();

$BlogItem = new BlogListItem();

$BID = 0;
if (isset($_GET["bid"]))
	$BID = intval($_GET["bid"]);
else if (isset($_POST["bid"]))
	$BID = intval($_POST["bid"]);

if (isset($_POST["msg_add"]) && isset($_SESSION["user"]))
{
	if ($BID == 0 || !isset($_POST["msg"])) die("非法操作~");
	if ($_POST["msg"] == "")
	{
		ClientAlert("输入不能为空");
	}
	else 
	{
		$msg = $_POST["msg"];
		$time = GetSysTime();
		$ip = GetRequestIP();
		if(strlen($_SESSION["user"]) > 10000)
			$user = substr($_SESSION["user"], 0, 10000);
		else 
			$user = $_SESSION["user"];

		$stmt = $mysqli->prepare("insert into msglist(bid,user,msg,in_ip,in_time) values(?,?,?,?,?)");
		$stmt->bind_param("issss", $BID, $user, $msg, $ip, $time);
		if (!$stmt->execute())
			ClientAlert("系统出错，添加留言失败~");
		$stmt->close();
	}
}

$result = $mysqli->query("select * from BlogList where id = $BID limit 0,1", MYSQLI_USE_RESULT);

if ($result)
{
	if ($row = $result->fetch_array(MYSQLI_ASSOC))
	{
		$BlogItem->id = $row["id"];
		$BlogItem->title = $row["title"];
		$BlogItem->content = $row["content"];
		$BlogItem->user = $row["user"];
		$BlogItem->keyword = substr($row["keyword"], 0, strlen($row["keyword"]) - 1);
		$BlogItem->in_ip = $row["in_ip"];
		$BlogItem->in_time = $row["in_time"];
		$BlogItem->permission = $row["permission"];
		$BlogItem->classlist = array();
	}
	$result->close();
}

$result = $mysqli->query("select id, name from classlist where type='algorithm' and id in (select cid from BCLinkList where bid = $BID and cid <> 0)", MYSQLI_USE_RESULT);

if ($result)
{
	while ($row = $result->fetch_array(MYSQLI_ASSOC))
	{
		$ClassItem = new ClassListItem();
		$ClassItem->id = $row["id"];
		$ClassItem->name = $row["name"];
		$BlogItem->classlist[] = $ClassItem;
	}
	$result->close();
}

if (count($BlogItem->classlist) == 0)
{
	$ClassItem = new ClassListItem();
	$ClassItem->id = 0;
	$ClassItem->name = "未分类";
	$BlogItem->classlist[] = $ClassItem;
}



$KeyArr = array();
$KeyWithList = array();
$Arr_len = 0;
if ($BlogItem->keyword != "")
{
	$KeyArr = explode("|", $BlogItem->keyword."|", 10);
	foreach ($KeyArr as $Key)
	{
		if ($Key == "")
			continue;
		$result = $mysqli->query("select id,title,user from bloglist where keyword like \"%".$Key."|%\" limit 0, 10", MYSQLI_USE_RESULT);
		if ($result)
		{
			while($row = $result->fetch_array(MYSQLI_ASSOC))
			{
				$BlogListItem = new BlogListItem();
				$BlogListItem->id = $row["id"];
				$BlogListItem->title = $row["title"];
				if (strlen($BlogListItem->title) > 10)
					$BlogListItem->title = StrCutUTF8($BlogListItem->title, 10)."...";

				$BlogListItem->user = $row["user"];

				if ($BlogListItem->id == $BlogItem->id)
					continue;

				$find = true;
				foreach ($KeyWithList as $Item)
				{
					if ($Item->id == $BlogListItem->id)
					{
						$find = false;
						break;
					}
				}
				if ($find)
				{
					++$Arr_len;
					$KeyWithList[] = $BlogListItem;
				}
				if ($Arr_len >= $blog_same_list_max_cnt)
					break;
			}

			$result->close();
			if ($Arr_len >= $blog_same_list_max_cnt)
					break;
		}
	}
}

$MsgList = array();
$result = $mysqli->query("select * from msglist where bid = $BID", MYSQLI_USE_RESULT);
if ($result)
{
	while($row = $result->fetch_array(MYSQLI_ASSOC))
	{
		$MsgItem = new MsgListItem();
		$MsgItem->id = $row["id"];
		$MsgItem->bid = $row["bid"];
		$MsgItem->user = $row["user"];
		$MsgItem->msg = $row["msg"];
		$MsgItem->in_ip = $row["in_ip"];
		$MsgItem->in_time = $row["in_time"];
		$MsgItem->extra = intval($row["extra"]);
		$MsgList[] = $MsgItem;
	}
	$result->close();
}

$mysqli->close();

?>